from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from message.models import ClassMessage
import string
import random
# from django.db.models.fields.related_descriptors import ReverseOneToOneDescriptor

# Create your models here.

class Invitations(models.Model):
	code = models.CharField(default=None, max_length=20)
	itsname = models.CharField(max_length=10)
	cs = models.IntegerField()

class UserInfo(models.Model):
	user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True)
	theme = models.CharField(max_length=20)

class RenameCard(models.Model):
	code = models.CharField(max_length=20)
	number_for_times = models.IntegerField()

class MemeQuestions(models.Model):
	question = models.CharField(max_length=100)
	answer = models.CharField(max_length=20)

@receiver(post_save, sender=User)
def save_userinfo(sender, instance, created, **kwargs):
	if not created:
		try:
			if instance.userinfo == None:
				new_userinfo = UserInfo(theme="default")
				instance.userinfo = new_userinfo
				instance.userinfo.save()
			else:
				instance.userinfo.save()
		except:
			new_userinfo = UserInfo(theme="default")
			instance.userinfo = new_userinfo
			instance.userinfo.save()
